package com.example.iotest.bio;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicReference;

public class Test3 {
    //    private static B b=new B("1");
    static Test3 t = new Test3();

    public static void main(String[] args) {
        /*HashMap<String, Integer> map = new HashMap<>();
        map.put("1",1);
        map.put("2",2);
        map.put("3",3);
        map.put("4",4);
        map.put("5",5);
        map.put("6",6);
        map.put("7",7);
        map.put("8",8);
        map.put("9",9);
        map.put("10",10);
//        map.put("11",11);
//        map.put("11",11);
//        map.put("11",11);
//        map.put("11",11);
//        map.put("11",11);
//        map.put("11",11);
//        map.put("11",11);
//        map.put("11",11);
//        map.put("11",11);
//        map.put("11",11);
//        map.put("11",11);
        map.put("12",11);
        map.put("14",11);
        map.put("15",11);
        map.put("16",11);
        map.put("17",11);
        map.put("18",11);
        map.put("19",11);
        map.put("20",11);
        for (Map.Entry<String, Integer> entry:
                map.entrySet()){
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
        synchronized (map){
            new Thread(()->{
                System.out.println("map.put(\"21\",21) "+map.put("21",21));
                System.out.println("21: "+map.get("21"));
                try {
                    Thread.sleep(2100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        synchronized (map){
            new Thread(()->{
                System.out.println("map.put(\"22\",22) "+map.put("22",22));
                System.out.println("22: "+map.get("22"));
                try {
                    Thread.sleep(2100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
//        Thread.sleep(100);
        for (Map.Entry<String, Integer> entry:
        map.entrySet()){
            System.out.println(entry.getKey()+" "+entry.getValue());
        }*/

        /*AtomicReference<B> b= new AtomicReference<>(new B("1"));
        Lock lock=new ReentrantLock();
        synchronized (t){
            new Thread(()->{
                b.set(new B("2"));
                System.out.println(b);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
//            b.setS("2");
//            System.out.println(b);
        }
        synchronized (t){
            new Thread(()->{
                b.set(new B("3"));
                System.out.println(b);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();*/
//            b.setS("3");
//            System.out.println(b);
//        }
        Test3 test3 = new Test3();
        test3.f2();
        test3.f3();
    }


    final Object o = new Object();

    @SneakyThrows
    void f2() {
        synchronized (o) {
            new Thread(() -> {
                System.out.println(2);
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    @SneakyThrows
    void f3() {
        synchronized (o) {
            new Thread(() -> {
                System.out.println(3);
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    synchronized void f4() {
        System.out.println(3);
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    String f5(){
        new Thread(()->{
            try {
                Thread.sleep(5000);
                System.out.println("测试是否在over后");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        return "over";
    }

    @Test
    public void f1() {
        AtomicReference<B> b = new AtomicReference<>(new B("1"));
        synchronized (b) {
            new Thread(() -> {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                b.set(new B("2"));
                System.out.println(b);
            }).start();
            b.get().setS("2");
            System.out.println(b);
        }
        synchronized (b) {
            new Thread(() -> {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                b.set(new B("3"));
                System.out.println(b);
            }).start();
            b.get().setS("3");
            System.out.println(b);
        }
//        System.out.println(b);
//        b.setS("");
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class B {
    private String s;

    public static void main(String[] args) {
        Test3 test3 = new Test3();
        Thread t1 = new Thread(test3::f4);
        Thread t2 = new Thread(test3::f4);
        t1.start();
        t2.start();
        String s = test3.f5();
        System.out.println(s);

    }
}
